//
//  PTGuideViewController.m
//  Receipt
//
//  Created by admin on 2019/3/29.
//  Copyright © 2019 LG. All rights reserved.
//

#import "TTGuideViewController.h"
#import "TYCyclePagerView.h"
#import "TYPageControl.h"
#import "TYCyclePagerViewCell.h"
#import "TTGuideViewCell.h"
#import "LLTabbarViewController.h"

static NSString *const guideCellIdentifier = @"guideCellIdentifier";

@interface TTGuideViewController ()<TYCyclePagerViewDataSource, TYCyclePagerViewDelegate>
{
    CGFloat dismissBtnOriginY;
    CGFloat dismissBtnWidth;
}
@property (nonatomic, strong) TYCyclePagerView *pagerView;
@property (nonatomic, strong) TYPageControl *pageControl;
@property (nonatomic, strong) NSArray *datas;
@property (nonatomic, strong) UIButton *dismissBtn;

@end

@implementation TTGuideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (SCREEN_HEIGHT > 896) {
        // ipad
        self.datas = @[@"1-ipad", @"2-ipad", @"3-ipad"];
        dismissBtnOriginY = SCREEN_HEIGHT * 0.9;
        dismissBtnWidth = 200;
    } else if (SCREEN_HEIGHT > 736) {
        // x 系列
        self.datas = @[@"1-x", @"2-x", @"3-x"];
        dismissBtnOriginY = SCREEN_HEIGHT * 0.85;
        dismissBtnWidth = 140;
    } else {
        self.datas = @[@"1-8p", @"2-8p", @"3-8p"];
        dismissBtnOriginY = SCREEN_HEIGHT * 0.88;
        dismissBtnWidth = 140;
    }
    
    
    [self addPagerView];
//    [self addPageControl];
    
    [self.pagerView reloadData];
    
    // 添加开始体验按钮
    [self addDismissBtn];
    
}

#pragma mark - TYCyclePagerViewDataSource
- (NSInteger)numberOfItemsInPagerView:(TYCyclePagerView *)pageView {
    return self.datas.count;
}

- (UICollectionViewCell *)pagerView:(TYCyclePagerView *)pagerView cellForItemAtIndex:(NSInteger)index {
    
    TTGuideViewCell *cell = [pagerView dequeueReusableCellWithReuseIdentifier:guideCellIdentifier forIndex:index];
    cell.imageName = self.datas[index];
    return cell;
}

- (TYCyclePagerViewLayout *)layoutForPagerView:(TYCyclePagerView *)pageView {
    TYCyclePagerViewLayout *layout = [[TYCyclePagerViewLayout alloc]init];
    layout.itemSize = CGSizeMake(pageView.width, pageView.height);
    layout.itemHorizontalCenter = YES;  // 居中显示
    layout.itemSpacing = 10;
    layout.layoutType = TYCyclePagerTransformLayoutNormal;
    layout.minimumScale = 0.8;
    return layout;
}

- (void)pagerView:(TYCyclePagerView *)pageView didScrollFromIndex:(NSInteger)fromIndex toIndex:(NSInteger)toIndex {
    _pageControl.currentPage = toIndex;
    self.dismissBtn.hidden = toIndex != self.datas.count - 1;
}

- (void)pagerView:(TYCyclePagerView *)pageView didSelectedItemCell:(__kindof UICollectionViewCell *)cell atIndex:(NSInteger)index
{
    NSLog(@"选中了 -->%ld", index);
    if (index == self.datas.count - 1) {
        // 消失
        [UIApplication sharedApplication].keyWindow.rootViewController = [[LLTabbarViewController alloc] init];
    }
}

- (void)pagerViewWillBeginDecelerating:(TYCyclePagerView *)pageView
{
    CGFloat x = pageView.contentOffset.x;
    NSLog(@">>adsfadsgf>>>  %f", x);
    if (x >= SCREEN_WIDTH * (self.datas.count - 1) + SCREEN_WIDTH * 0.1) {
        // 消失
        [UIApplication sharedApplication].keyWindow.rootViewController = [[LLTabbarViewController alloc] init];
    }
}


- (void)addPagerView
{
    TYCyclePagerView *pagerView = [[TYCyclePagerView alloc]init];
    pagerView.frame = self.view.bounds;
    pagerView.isInfiniteLoop = NO;
    pagerView.dataSource = self;
    pagerView.delegate = self;
    [pagerView registerClass:[TTGuideViewCell class] forCellWithReuseIdentifier:guideCellIdentifier];
    [self.view addSubview:pagerView];
    self.pagerView = pagerView;
}

//- (void)addPageControl
//{
//    TYPageControl *pageControl = [[TYPageControl alloc]init];
//    pageControl.frame = CGRectMake(0, self.view.bottom - 80, self.view.width, 30);
//    pageControl.currentPageIndicatorSize = CGSizeMake(10, 10);
//    pageControl.pageIndicatorSize = CGSizeMake(8, 8);
//    pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
//    pageControl.pageIndicatorTintColor = UIColorFromRGB(0xD0D0D0);
//    pageControl.numberOfPages = self.datas.count;
//    [self.view addSubview:pageControl];
//    self.pageControl = pageControl;
//}


- (void)dismissAction
{
    // 消失
    [UIApplication sharedApplication].keyWindow.rootViewController = [[LLTabbarViewController alloc] init];
}

- (void)addDismissBtn
{
    UIButton *dismissBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    dismissBtn.frame = CGRectMake((SCREEN_WIDTH - dismissBtnWidth) * 0.5, dismissBtnOriginY, dismissBtnWidth, 60);
    [dismissBtn addTarget:self action:@selector(dismissAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:dismissBtn];
    self.dismissBtn = dismissBtn;
    
}

@end
